<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsNewsBean {
	
	public function __construct() {
		//
	}
		
	public function __destruct() {
		//
	}
	  
    public static function get_all_news($db) {
		$strSQL = sprintf("SELECT * FROM %s", TBL_MODULE_NEWS);
		return $db->get_results($strSQL);
    }

    
    public static function get_all_news_paging($db, $paging,$path) {
        $page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
        $rows_per_page= $paging;
        $page_start = ( $page - 1 ) * $rows_per_page;
        $page_end = $page * $rows_per_page;
        $strSQL = sprintf("SELECT * FROM %s LIMIT $page_start,$rows_per_page ", TBL_MODULE_NEWS);
        return $db->get_results($strSQL);
    }
    
	public static function get_news_by_id($db, $id){
		$sql = "SELECT * FROM " . TBL_MODULE_NEWS . " WHERE news_id  = '$id'";
		return $db->get_row($sql);
    }
	
	public static function save($db, $newsData) {
		$sql = sprintf("INSERT INTO " . TBL_MODULE_NEWS . 
									" (news_name,news_description, news_image_small,news_image_large,created_date,".
									"creator_id,position,active) " .
									"values ('%s', '%s', '%s', '%s', now(), '%s', '%s', '%s') ", 
									$newsData['news_name'], 
									$newsData['news_description'], 
									$newsData['news_image_small'], 
									$newsData['news_image_large'], 
									$newsData['creator_id'], 
									$newsData['position'], 
									$newsData['active']);
		$res = $db->query($sql);
    	if($res == true) {
    		return $db->insert_id;
    	} else
    		return -1;	
	}
	
	public function update($newsData) {
		$sql = mysql_query(sprintf("UPDATE " . TBL_MODULE_NEWS . " SET " .
			"news_name = '%s',news_description = '%s',news_image_small = '%s',news_image_large = '%s',active = '%s' ".
			"WHERE news_id = '%s'", 
			$newsData['news_name'], 
			$newsData['news_description'], 
			$newsData['news_image_small'], 
			$newsData['news_image_large'], 
			$newsData['active'],
			$newsData['news_id']));
	}
	
	public function delete($id) {
		$sql = mysql_query("DELETE FROM " . TBL_MODULE_NEWS . " WHERE news_id = '$id'");
	}
	
    public static function count_all_news($db) {
        $strSQL = sprintf("SELECT COUNT(%1\$s.news_id) as total FROM %1\$s ", TBL_MODULE_NEWS);
        $res = $db->get_row($strSQL);
		return	$res->total;
    }
}
?>